package com.shareach.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.benayn.ustyle.Reflecter;
import com.google.common.collect.Maps;
import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.redis.impl.DeployRDao;
import com.shareach.dao.redis.impl.DeployRDao.DeployType;
import com.shareach.domain.BaseCityDomain;
import com.shareach.domain.BaseZoomDomain;
import com.shareach.service.BaseCityService;
import com.shareach.service.BaseZoomService;
import com.shareach.util.JsonUtil;
import com.shareach.web.basic.Answer;

@Controller("wzoom")
@RequestMapping("/wzoom")
public class ZoomController extends BaseController {
	@Autowired
	private BaseZoomService zoomService;
	@Autowired
	private BaseCityService cityService;
	@Autowired
	private DeployRDao deployRDao;
	
	@RequestMapping(value = "/index.htm")
	public ModelAndView summary(HttpServletRequest request, HttpServletResponse response, String name, String city) {
		ModelAndView mv = new ModelAndView("admin/shop/zoom/index");

		BaseQueryOrder order = new BaseQueryOrder();
		order.setNumber(getPageNo(request).toString());
		order.setPageSize(getPageSize(request));
		order.addSort("_id", Order.DESCENDING.toString());
		BaseZoomDomain zoomDomain = new BaseZoomDomain();

		if (!StringUtils.isEmpty(name)) {
			zoomDomain.setName(name);
			mv.addObject("name", name);
		}

		if (!StringUtils.isEmpty(city)) {
			zoomDomain.setCityId(Long.valueOf(city));
		}

		Page<BaseZoomDomain> result = zoomService.getPageResult(zoomDomain, order);

		List<BaseCityDomain> cities = cityService.findAll();
		mv.addObject("cities", cities);
		mv.addObject("result", result);

		return mv;
	}

	@RequestMapping(value = "/detail.htm")
	public ModelAndView detail(HttpServletRequest request, Long zoomId) {
		ModelAndView mv = new ModelAndView("admin/shop/zoom/detail");

		// zoom
		BaseZoomDomain domZoom = zoomService.getById(zoomId);
		Map<String, Object> zoom = asDynabean(domZoom);
		mv.addObject("zoom", zoom);
		return mv;
	}

	@RequestMapping(value = "/upload.htm", method = RequestMethod.POST)
	public ModelAndView upload(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("file") MultipartFile file) {
		if (!file.isEmpty()) {
			String newFileName = uploadFile(request, file, DeployType.Place.getDesc());

			// update shop
			Long zoomId = Long.valueOf(request.getParameter("zoomId"));
			BaseZoomDomain zoomDomain = new BaseZoomDomain();
			zoomDomain.setId(zoomId);
			zoomDomain.setIcon(DeployType.Place.getDesc() + "/" + newFileName);
			zoomService.update(zoomDomain);
			resetRedisDeployed(DeployType.Place, zoomId);
			return new ModelAndView(new RedirectView(request.getContextPath() + "wzoom/detail.htm?zoomId=" + zoomId));
		}
		return new ModelAndView();

	}

	@RequestMapping(value = "/edit.htm")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, String zoomId,
			BaseZoomDomain domain) {
		ModelAndView view = new ModelAndView("admin/shop/zoom/edit");
		if (domain == null || domain.getId() == null) {
			// redirect
			Long parabrand = Long.valueOf(zoomId);
			BaseZoomDomain zoom = zoomService.getById(parabrand);
			view.addObject("zoom", zoom);
			return view;
		} else {
			Answer<Object> r = new Answer<Object>();
			try {
				BaseZoomDomain zoom = zoomService.getById(domain.getId());
				BaseZoomDomain change = zoom.compareTo(domain);
				change.setId(domain.getId());
				// edit
				domain.setName(null);// 此属性不可更新
				zoomService.update(change);
				// update redis
				resetRedisDeployed(DeployType.Place, domain.getId());
				this.outText(response, JsonUtil.jsonFromObject(r));
			} catch (Exception e) {
				r.setCmd(-1);
				r.setMsg("System Error");
				this.outText(response, JsonUtil.jsonFromObject(r));
				e.printStackTrace();
			}
			return view;
		}
	}

	private Map<String, Object> asDynabean(Object obj) {
		if (null == obj) {
			return Maps.newHashMap();
		}
		return Reflecter.from(obj).asMap();
	}
	public void resetRedisDeployed(DeployType type, Long id) {
		if (deployRDao.isDeployed(type, id)) {
			deployRDao.setUndeployed(type, id);
		}
	}
}
